package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.entity.Article;
import com.example.utils.ExceptionResult;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

import com.example.service.IRemarkService;
import com.example.entity.Remark;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dengc
 * @since 2023-05-16
 */
@RestController
@RequestMapping("/remark")
public class RemarkController {

        @Resource
        private IRemarkService remarkService;

        //新增与修改同一个方法
        @PostMapping
        public ExceptionResult save(@RequestBody Remark remark) {
            return ExceptionResult.success(remarkService.saveOrUpdate(remark));
        }

        //删除
        @DeleteMapping("/{id}")
        public ExceptionResult delete(@PathVariable Integer id) {
            return ExceptionResult.success(remarkService.removeById(id));
        }

        //查询列表
        @GetMapping("/list/{articleid}")
        public ExceptionResult findAll(@PathVariable Integer articleid) {
            return ExceptionResult.success(remarkService.findRemark(articleid));
        }

        //根据id查询单个
        @GetMapping("/{id}")
        public ExceptionResult findOne(@PathVariable Integer id) {
            return ExceptionResult.success(remarkService.getById(id));
        }

        //批量删除
        @PostMapping("/del/batch")
        public ExceptionResult deleteBatch(@RequestBody List<Integer> ids){
                return ExceptionResult.success(remarkService.removeByIds(ids));
        }

        //分页查询
        @GetMapping("/page")
        public ExceptionResult findPage(@RequestParam Integer pageNum,
                                        @RequestParam Integer pageSize,
                                        @RequestParam(defaultValue = "") String userid,
                                        @RequestParam(defaultValue = "") String articleid) {
                LambdaQueryWrapper<Remark> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.like(!"".equals(userid), Remark::getUserid,userid);
                queryWrapper.like(!"".equals(articleid), Remark::getArticleid,articleid);
                return ExceptionResult.success(remarkService.page(new Page<>(pageNum, pageSize),queryWrapper));
        }
}
